home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / XML / Beautifier / Renderer.php next >
PHP Script  |  2004-03-24  |  6KB  |  188 lines

  1. <?PHP
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stephan Schmidt <schst@php.net>                             |
  17. // +----------------------------------------------------------------------+
  18.  
  19. /**
  20.  * XML/Beautifier/Renderer.php
  21.  *
  22.  * @category XML
  23.  * @package  XML_Beautifier
  24.  * @author   Stephan Schmidt <schst@php.net>
  25.  */
  26.  
  27. /**
  28.  * Renderer base class for XML_Beautifier
  29.  *
  30.  * @category XML
  31.  * @package  XML_Beautifier
  32.  * @author   Stephan Schmidt <schst@php.net>
  33.  */
  34. class XML_Beautifier_Renderer {
  35.  
  36.    /**
  37.     * options
  38.     * @var array
  39.     */
  40.     var $_options = array();
  41.  
  42.    /**
  43.     * create a new renderer
  44.     *
  45.     * @access   public
  46.     * @param    array   options for the serialization
  47.     */
  48.     function XML_Beautifier_Renderer($options = array())
  49.     {
  50.         $this->_options = $options;
  51.     }
  52.     
  53.    /**
  54.     * Serialize the XML tokens
  55.     *
  56.     * @access   public
  57.     * @param    array   XML tokens
  58.     * @return   string  XML document
  59.     * @abstract
  60.     */
  61.     function serialize($tokens)
  62.     {
  63.         return  '';
  64.     }
  65.  
  66.    /**
  67.     * normalize the XML tree
  68.     *
  69.     * When normalizing an XML tree, adjacent data sections
  70.     * are combined to one data section.
  71.     *
  72.     * @access  public
  73.     * @param   array       XML tree as returned by the tokenizer
  74.     * @return  array       XML tree
  75.     */
  76.     function normalize($tokens)
  77.     {
  78.         $tmp    =   array();
  79.         foreach ($tokens as $token) {
  80.             array_push($tmp, $this->_normalizeToken($token));
  81.         }
  82.         return $tmp;
  83.     }
  84.  
  85.    /**
  86.     * normalize one element in the XML tree
  87.     *
  88.     * This method will combine all data sections of an element.
  89.     *
  90.     * @access   private
  91.     * @param    array   $struct
  92.     * @return   array   $struct
  93.     */
  94.     function _normalizeToken($token)
  95.     {
  96.         if ((isset($token["children"])) && !is_array($token["children"]) || empty($token["children"])) {
  97.             return $token;
  98.         }
  99.  
  100.         $children = $token["children"];
  101.         $token["children"] = array();
  102.         $cnt = count($children);
  103.         $inCData = false;
  104.         for ($i = 0; $i < $cnt; $i++ )
  105.         {
  106.             // no data section
  107.             if ($children[$i]["type"] != XML_BEAUTIFIER_CDATA) {
  108.                 $children[$i] = $this->_normalizeToken($children[$i]);
  109.  
  110.                 $inCData = false;
  111.                 array_push($token["children"], $children[$i]);
  112.                 continue;
  113.             }
  114.  
  115.             /**
  116.             * remove whitespace
  117.             */
  118.             if( $this->_options['removeLineBreaks'] == true )
  119.             {
  120.                 $children[$i]['data'] = trim($children[$i]['data']);
  121.                 if( $children[$i]['data'] == '' ) {
  122.                     continue;
  123.                 }
  124.             }
  125.  
  126.             if ($inCData) {
  127.                 $tmp = array_pop($token["children"]);
  128.  
  129.                 if( $children[$i]['data'] != '' ) {
  130.                     if( $tmp['data'] != '' && $this->_options['removeLineBreaks'] == true ) {
  131.                         $tmp['data'] .= ' ';
  132.                     }
  133.                     $tmp["data"] .= $children[$i]["data"];
  134.                 }
  135.                 array_push($token["children"], $tmp);
  136.             } else {
  137.                 array_push($token["children"], $children[$i]);
  138.             }
  139.  
  140.             $inCData = true;
  141.         }
  142.         return $token;
  143.     }
  144.  
  145.    /**
  146.     * indent a text block consisting of several lines
  147.     *
  148.     * @access private
  149.     * @param  string    $text   textblock
  150.     * @param  integer   $depth  depth to indent
  151.     * @param  boolean   $trim   trim the lines
  152.     * @return string            indented text block
  153.     */
  154.     function _indentTextBlock($text, $depth, $trim = false)
  155.     {
  156.         $indent = $this->_getIndentString($depth);
  157.         $tmp = explode("\n", $text);
  158.         $cnt = count($tmp);
  159.         $xml = '';
  160.         for ($i = 0; $i < $cnt; $i++ ) {
  161.             if ($trim) {
  162.                 $tmp[$i] = trim($tmp[$i]);
  163.             }
  164.             if( $tmp[$i] == '' )
  165.                 continue;
  166.             $xml .= $indent.$tmp[$i].$this->_options["linebreak"];
  167.         }
  168.         return $xml;
  169.     }
  170.     
  171.    /**
  172.     * get the string that is used for indentation in a specific depth
  173.     *
  174.     * This depends on the option 'indent'.
  175.     *
  176.     * @access private
  177.     * @param  integer   $depth  nesting level
  178.     * @return string            indent string
  179.     */
  180.     function _getIndentString($depth)
  181.     {
  182.         if ($depth > 0) {
  183.             return str_repeat($this->_options["indent"], $depth);
  184.         }
  185.         return "";
  186.     }
  187. }
  188. ?>